const { songs } = require('../../data/songs.js');

Page({
  data: {
    playlist: {
      cover: songs[0].cover,
      title: '我喜欢的音乐',
      creatorName: '用户',
      creatorAvatar: 'https://img.js.design/assets/img/62c8f6b4c1e8d2765e04bcfb.webp',
      description: '这是一个充满活力的歌单，收录了最近喜欢的音乐。',
      commentCount: 1660,
      shareCount: 7732,
      songCount: songs.length,
      songs: songs.slice(0, 10).map(song => ({
        id: song.id,
        name: song.title,
        artist: song.artist,
        album: song.album,
        duration: song.duration,
        cover: song.cover,
        src: song.src
      }))
    }
  },

  onLoad(options) {
    // 这里可以根据options.id加载不同的歌单
    if (options.type === 'liked') {
      this.setData({
        'playlist.cover': 'https://img.js.design/assets/img/62c8f65c43eecc00bbccfa40.webp'
      });
    }
  },

  // 返回上一页
  onBackTap() {
    wx.navigateBack({
      delta: 1
    });
  },

  // 播放全部
  onPlayAllTap() {
    const firstSong = this.data.playlist.songs[0];
    if (firstSong) {
      this.playSong(firstSong);
    }
  },

  // 点击单首歌曲
  onSongTap(e) {
    const song = e.currentTarget.dataset.song;
    if (song) {
      this.playSong(song);
    }
  },

  // 点击更多按钮
  onMoreTap(e) {
    const song = e.currentTarget.dataset.song;
    wx.showActionSheet({
      itemList: ['收藏到歌单', '分享', '下载'],
      success(res) {
        console.log(res.tapIndex);
      }
    });
  },

  // 播放��曲
  playSong(song) {
    const app = getApp();
    const playerManager = app.globalData.playerManager;
    if (playerManager) {
      // 构建完整的歌曲对象
      const fullSong = {
        id: song.id,
        title: song.name,
        artist: song.artist,
        album: song.album,
        cover: song.cover,
        src: song.src,
        duration: song.duration
      };

      // 播放音频
      playerManager.playSong(fullSong);
      
      // 跳转到播放页面
      wx.navigateTo({
        url: '/pages/player/player'
      });
    }
  }
}); 